package com.appbee.gdata.picasa.client.ui;

import javax.inject.Inject;

import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.process.LoadingIndcator;
import com.appbee.gdata.picasa.client.activity.PicasaPhotosActivity;
import com.appbee.gdata.picasa.client.text.PicasaStrings;
import com.appbee.gdata.picasa.client.ui.design.PhotosListResources;
import com.appbee.gdata.picasa.shared.SimplePhotoProxy;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class PhotosView extends Composite implements PicasaPhotosActivity.View
{

	private static PhotosViewUiBinder uiBinder = GWT
			.create(PhotosViewUiBinder.class);

	interface PhotosViewUiBinder extends UiBinder<Widget, PhotosView>
	{
	}
	
	@UiField(provided=true)
	PageStyle style;

	@UiField(provided=true)
	CellList<SimplePhotoProxy> cellList;
	
	@UiField(provided=true)
	SimplePager pager;

	@UiField(provided=true)
	SimplePager pager2;
	
	@UiField
	Button backButton;

	private PicasaPhotosActivity activity;

	private final PicasaMediaHtmlTemplate template;

	private final SingleSelectionModel<SimplePhotoProxy> selectionModel;

	@UiField(provided = true)
	PicasaStrings picasaStrings;

	@Inject
	public PhotosView(PageStyle style,
						PhotosListResources photosListRes,
						PicasaStrings picasaStrings,
						PicasaMediaHtmlTemplate template,
						LoadingIndcator loadingIndcator)
	{
		this.style = style;
		this.picasaStrings = picasaStrings;
		this.template = template;
		selectionModel = new SingleSelectionModel<SimplePhotoProxy>();
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler()
		{
			
			@Override
			public void onSelectionChange(SelectionChangeEvent event)
			{
				SimplePhotoProxy selectedObject = selectionModel.getSelectedObject();
				if(selectedObject!=null)
					activity.onPhotoSelected(selectedObject);
			}
		});
		cellList = new CellList<SimplePhotoProxy>(new MediaThumbnailCell(),
				photosListRes);
		cellList.setLoadingIndicator(loadingIndcator);
		cellList.setSelectionModel(selectionModel);
		cellList.setPageSize(15);
		pager = new SimplePager();
		pager.setDisplay(cellList);
		pager2 = new SimplePager();
		pager2.setDisplay(cellList);
		initWidget(uiBinder.createAndBindUi(this));
		backButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				activity.doBack();
			}
		});
	}

	@Override
	public void setActivity(PicasaPhotosActivity activity)
	{
		this.activity = activity;
	}

	@Override
	public HasData<SimplePhotoProxy> getHasData()
	{
		return cellList;
	}

	@Override
	public void clearSelection()
	{
		SimplePhotoProxy selectedObject = selectionModel.getSelectedObject();
		if(selectedObject!=null)
			selectionModel.setSelected(selectedObject, false);
	}

	public class MediaThumbnailCell extends AbstractCell<SimplePhotoProxy>
	{
		public MediaThumbnailCell()
		{
			super("click");

		}
		@Override
		public void render(Context context, SimplePhotoProxy value, SafeHtmlBuilder sb)
		{
			if(value!=null)
			{
				sb.append(template.img(value.getTitle()==null?"":value.getTitle(), value.getThumbnail().getUrl()));
			}
		}
	}
}
